/*
 * ChatGroupManager.java
 *
 * Created on 2007年5月15日, 下午3:59
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.ecomm.chat.manager;

import com.ecomm.chat.pojo.ChatGroup;
import com.ecomm.platcore.service.BaseEntityManager;
import com.ecomm.util.CommonMethod;
import com.ecomm.util.DB;
import com.ecomm.util.DBFactory;
import com.ecomm.util.DateUtils;
import com.ecomm.util.HibernateUtil;
import com.ecomm.util.SpringUtil;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.hibernate.Session;

/**
 *
 * <p>Title: 易讯聊天系统</p>
 * <p>Description:
 *  易讯聊天系统
 * </p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: 联合易讯 </p>
 * @author <font color='blue'>pijunliang</font> &nbsp&nbsp<a href="mailto:pijunliang@hotmail.com">pijunliang@hotmail.com</a>
 * @version 1.0
 * @since  2007年5月15日, 下午3:59
 */
public class ChatGroupManager extends BaseEntityManager<ChatGroup> {
  
  /** Creates a new instance of ChatGroupManager */
  public ChatGroupManager() {
  }
  private static ChatGroupManager chatGroupManager = null;
  
  public static ChatGroupManager getInstance(){
    if(chatGroupManager == null){
      chatGroupManager = (ChatGroupManager)SpringUtil.getBean("chatGroupManager");
    }
    return chatGroupManager;
  }
  /**
   * 保存业务对象的函数.
   */
  public String doSaveEntity(ChatGroup object) {
    object.setCreatetime(DateUtils.getCurrTime());
    return (String) getHibernateTemplate().save(object);
  }
  /**
   * 保存业务对象的函数.
   */
  public void doUpdateEntity(ChatGroup object) {
    
    getHibernateTemplate().update(object);
  }
  /**
   *利用直连构造群组树
   */
  public List getGroupTree(String createUserid){
    if(createUserid== null || createUserid.equals("")){
      return null;
    }
    String hql = "from ChatGroup where cerateuserid = '"+createUserid+"'";
    return this.getHibernateTemplate().find(hql);
  }
  /**
   *通过ID，返回组的对象
   */
  public ChatGroup getChatGroup(String id){
    if(id== null || id.equals("")){
      return null;
    }
    return (ChatGroup)getHibernateTemplate().get(ChatGroup.class,id);
  }
  /**
   *通过群ID得到 群中的描述 和群名字
   */
  public String getChatGroupDescAndCreateuserName(String id){
    StringBuilder sb = new StringBuilder();
    ChatGroup cg = getChatGroup(id);
    if(cg == null ){
      return null;
    }
    sb.append(cg.getGroupname());
    sb.append("(群创建人：");
    sb.append(CommonMethod.getUserNameByUserID(cg.getCerateuserid()));
    sb.append("):");   
    sb.append(cg.getGroupdesc());
    return sb.toString();
  }
  /**
   *同上，只是缺少群描述
   */
  public String getChatGroupNameAndCreateuserName(String id){
    StringBuilder sb = new StringBuilder();
    ChatGroup cg = getChatGroup(id);
    if(cg == null ){
      return null;
    }
    sb.append(cg.getGroupname());
    sb.append("(群创建人：");
    sb.append(CommonMethod.getUserNameByUserID(cg.getCerateuserid()));
    sb.append(")");
    return sb.toString();
  }
}
